<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06_计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入数字1" id="i1" > <br>
<input type="text" placeholder="请输入数字2" id="i2" > <br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h4>运算结果:<span></span></h4>
<script>
    /*function f1(){
        i1.value = '';
        document.getElementById('i1').removeEventListener('click', f1);
    }
    function f2(){
        i2.value = '';
        document.getElementById('i2').removeEventListener('click', f2);
    }
    document.addEventListener('DOMContentLoaded', function() {
        document.getElementById('i1').addEventListener('click', f1);
        document.getElementById('i2').addEventListener('click', f2);
    });*/
    /*function add(){
        let a = parseFloat(i1.value);
        let b = parseFloat(i2.value);
        alert(a+b);
    }
    function sub(){
        let a = parseFloat(i1.value);
        let b = parseFloat(i2.value);
        alert(a-b);
    }
    function mul(){
        let a = parseFloat(i1.value);
        let b = parseFloat(i2.value);
        alert(a*b);
    }
    function div(){
        let a = parseFloat(i1.value);
        let b = parseFloat(i2.value);
        if(b==0){
            alert('除数不能为0');
            return;
        }
        alert(a/b);
    }*/
    function calc(op){
        //1.获取用户在输入框中输入的数字
        let n1 = document.getElementById('i1').value;
        let n2 = document.getElementById('i2').value;
        //2.将获取到的字符串转为数值类型
        n1 = parseFloat(n1);
        n2 = parseFloat(n2);
        //3.获取用来显示结果的span元素
        let spanE = document.querySelector('span');
        console.log(spanE)
        //4.根据用户选择的运算符,计算结果

        switch (op){
            case '+':
                spanE.innerHTML = n1 + n2;
                break;
            case '-':
                spanE.innerHTML = n1 - n2;
                break;
            case '*':
                spanE.innerHTML = n1*n2;
                break;
            case '/':
                if(n2==0){
                    alert('除数不能为0');
                }else {
                    spanE.innerHTML = n1/n2;
                    break;
                }

        }
    }
</script>
</body>
</html>